import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
import userSlice from './reducers/user';
// redux 持久化配置
import { persistStore, persistReducer, PersistConfig } from 'redux-persist';
// 将状态存储在本地
import storage from 'redux-persist/lib/storage';
// 合并reduce
import { combineReducers, Reducer } from 'redux';

// 持久化配置
const persistConfig: PersistConfig<any> = {
    key: 'redux-root',
    storage,
    blacklist: [] //黑名单不持久化存储哪些reducer
};
// 合并reducer
const combineReduxReducers = combineReducers({
    user: userSlice.reducer
});
// 设置持久化存储
const persistReduxReducer: typeof combineReduxReducers = persistReducer(persistConfig, combineReduxReducers);

// 创建一个Store
const store = configureStore({
    // reducer: {
    //     userStore: userSlice.reducer
    // }
    reducer: persistReduxReducer,
    middleware: (getDefaultMiddleware) =>
        getDefaultMiddleware({
            serializableCheck: false
        })
});

// 导出能持久化store
export const persistReduxStore = persistStore(store);

// 默认导出store
export default store;

/**
 * ReturnType ts 中的一个高阶类型，可以获取函数的返回值类型，使用时要使用函数签名
 */
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, RootState, unknown, Action<string>>;
